class Solution {
public:
    string multiply(string num1, string num2) {
        if(num1 == "0" || num2 == "0") return "0";
        int m = num1.size(), n = num2.size();
        vector<int> mul(m+n);  // 保存每位乘出来累加的结果
        string res;
        // 从低位开始模拟乘法
        for(int i = m-1; i >= 0; --i){
            for(int j = n-1; j >= 0; --j){
                int temp = (num1[i]-'0') * (num2[j]-'0');
                mul[i+j+1] += temp;    // mul[i+j+1] = num1[i]*num2[j];
            }
        }
        // 累加进位
        for(int i = m+n-1; i > 0; --i){
            mul[i-1] += mul[i] / 10;
            mul[i] %= 10;
        }
        // 判断是否有前导0；遍历存入结果字符串
        int index = mul[0]==0 ? 1 : 0;
        while(index < m+n){
            res.push_back(mul[index++]+'0');
        }
        
        return res;
    }
};